package 删掉一个元素以后全为1的最长子数组1493;

import java.util.ArrayList;

public class Solution {
    public int longestSubarray(int[] nums) {
        ArrayList<Integer> list = new ArrayList<>();
        int index = -1;
        for (int i = 0; i < nums.length; i++) {
            if(nums[i] == 0) {
                list.add(i - index - 1);
                index = i;
            }
        }
        if(nums.length - index - 1 != 0) {
            list.add(nums.length - index - 1);
        }
        if (list.isEmpty()) {
            return 0;
        }
        if(list.size() == 1) {
            return (Math.max(list.get(0) - 1, 0));
        }
        int max = 0;
        for (int i = 1; i < list.size(); i++) {
            max = Math.max(max, list.get(i) + list.get(i - 1));
        }
        return max;
    }
}
